﻿Imports System.Data.SqlClient

Public Class adm_merchantnews
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            If (Request("uid") <> Nothing And Request("uid") <> "") Then
                Dim title, content, strErr, memberId As String
                Dim companyId As String = ""
                Dim createDate
                createDate = Now
                strErr = ""
                memberId = Request("uid")
                Dim conn As SqlConnection = objfind.getConn()

                If (Request("editType") <> Nothing And Request("editType") <> "") Then
                    If (Request("editType").Equals("delete")) Then
                        ps.strSql = "delete from company_news where id=@id"
                        ps.parameters.Clear()
                        ps.addParameter("id", Request("newsId"))
                        objfind.es(ps, strErr)
                        MsgBox("successfully delete！")
                        Response.Redirect("adm_merchantnewslist.aspx?uid=" & Request("uid") & "")
                    End If
                ElseIf (Request("newsId") <> Nothing And Request("newsId") <> "") Then
                    ps.strSql = "select * from company_news where id=@id"
                    ps.parameters.Clear()
                    ps.addParameter("id", Request("newsId"))
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    If (objrd.Read()) Then
                        c_id.Value = Request("newsId")
                        c_title.Text = objrd.Item("title")
                        merchantnews.InnerHtml = objrd.Item("context")
                    End If
                End If
                If (Request("submitmerchantnews") <> Nothing And Request("submitmerchantnews") <> "") Then
                    If (Request("c_title") = "" Or Request("c_title") = Nothing) Then
                        Me.c_title_label.Text = "please enter title！"
                        objfind.closeConn(conn)

                        Exit Sub
                    Else
                        Me.c_title_label.Text = ""
                    End If
                    title = Request("c_title")
                    content = Request("c_content")
                    ps.strSql = "select * from merchant where owner_id =@ownerId"
                    ps.addParameter("ownerId", memberId)
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    If (objrd.Read()) Then
                        companyId = objrd.Item("company_id")
                    End If
                    If (Request("c_id") <> Nothing And Request("c_id") <> "") Then
                        ps.strSql = "update company_news set title=@title, context=@context where id=@id"
                        ps.parameters.Clear()
                        ps.addParameter("title", title)
                        ps.addParameter("context", content)
                        ps.addParameter("id", Request("c_id"))
                        objfind.es(ps, strErr)
                    Else
                        ps.strSql = "insert into company_news(company_id,lang_flag,title,context,create_date)" & _
                            "values(@companyId,'en',@title,@context,@createDate)"
                        ps.parameters.Clear()
                        ps.addParameter("companyId", companyId)
                        ps.addParameter("title", title)
                        ps.addParameter("context", content)
                        ps.addParameter("createDate", createDate)
                        objfind.es(ps, strErr)
                    End If
                    Response.Redirect("adm_merchantnewslist.aspx?uid=" & Request("uid") & "")
                End If
                objfind.closeConn(conn)

                Else
                    Response.Redirect("../memberMngt_default.aspx")
                End If
        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub

End Class